package com.inspur.edp.common.expr.visitor.construct;


import com.inspur.edp.common.expr.spi.ConstructVisitor;
import com.inspur.edp.common.expr.visitor.VisitResult;
import com.inspur.edp.common.type.DecorationOption;
import com.inspur.edp.common.type.Type;
import com.inspur.edp.common.type.TypeRefer;
import com.inspur.edp.common.type.utils.TypeUtil;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/7/16 16:56
 */
public class JavaSpringBeanConstructVisitor implements ConstructVisitor {

    public static final String MODE = "springBean";

    @Override
    public String getType() {
        return MODE;
    }

    @Override
    public VisitResult visit(TypeRefer typeRefer, Type type, String parameters) {
        DecorationOption option = type.getConstructorDecoration();
        String typeUrl = type.getTypeOptCode();
        String className = TypeUtil.getTypeCode(typeUrl);
        VisitResult visitResult = null;
        String beanId = option.getOptionMap().get("beanId");
        if (beanId == null) {
            visitResult = new VisitResult(String.format("((%s)SpringBeanUtils.getBean(%s.class))", className, className));
        } else {
            visitResult = new VisitResult(String.format("(%s)SpringBeanUtils.getBean(%s)", className, beanId));
        }
        visitResult.addImport(typeUrl);
        visitResult.addImport("io.iec.edp.caf.commons.utils.SpringBeanUtils");
        return visitResult;
    }
}
